﻿using Common.Data;
using CoreApi.Meta;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Runtime.Caching;

namespace CoreApi.Impl
{
    public class SimpleDataCollect:IDataCollect
    {
        string _conName = string.Empty;
        MemoryCache memoryCache = MemoryCache.Default;
        int cacheTime = 30;
        public SimpleDataCollect()
        {
            this._conName = ConfigurationManager.AppSettings["daq.connectionStringName"] ?? _conName;
        }

        private ReturnValue CollectAlarmSignal(AlarmSignal data, string signalType)
        {
            string key = string.Format("ccas_{0}.{1}.{2}", signalType, data.BrandId, data.DeviceSN);
            if (!data.Testing)
            {
                object cacheData = memoryCache.Get(key);
                if (cacheData != null) return cacheData as ReturnValue;
            }
            DbColumn[] columns = new DbColumn[]{
                new DbColumn("BrandId",data.BrandId)
                ,new DbColumn("DeviceSN",data.DeviceSN)
                ,new DbColumn("DataNumber",data.DataNumber??"")
                ,new DbColumn("MeasureTime",DbType.DateTime,data.MeasureTime)
                ,new DbColumn("Attribute",data.Attribute)
                ,new DbColumn("Testing",DbType.Boolean,data.Testing)
            };
            Database db = DatabaseFactory.CreateDatabase(_conName);
            using (TransactionScope tx = new TransactionScope())
            {
                string tb = signalType.Equals(AlarmType.GasAlarm) ? SQL.TABLE.RW_GA : SQL.TABLE.RW_SA;
                int rds = DbHelper.ExecuteInsert(db, null, tb, columns);
                tx.Complete();
                if (rds > 0 && !data.Testing) memoryCache.Add(key, new ReturnValue(), DateTimeOffset.UtcNow.AddMinutes(cacheTime));
            }
            return new ReturnValue();
        }

        public ReturnValue CollectBloodPressureMeasureData(BloodPressureMeasureData data)
        {
            DbColumn[] columns = new DbColumn[]{
                new DbColumn("BrandId",data.BrandId)
                ,new DbColumn("DeviceSN",data.DeviceSN)
                ,new DbColumn("DataNumber",data.DataNumber??"")
                ,new DbColumn("MeasureTime",DbType.DateTime,data.MeasureTime)
                ,new DbColumn("Sbp",DbType.Int32,data.Sbp)
                ,new DbColumn("Dbp",DbType.Int32,data.Dbp)
                ,new DbColumn("Attribute",data.Attribute)
            };
            Database db = DatabaseFactory.CreateDatabase(_conName);
            using (TransactionScope tx = new TransactionScope())
            {
                int rds = DbHelper.ExecuteInsert(db, null, SQL.TABLE.RW_BP, columns);
                tx.Complete();
            }
            return new ReturnValue();
        }

        public ReturnValue CollectGlycemicMeasureData(GlycemicMeasureData data)
        {
            DbColumn[] columns = new DbColumn[]{
                new DbColumn("BrandId",data.BrandId)
                ,new DbColumn("DeviceSN",data.DeviceSN)
                ,new DbColumn("DataNumber",data.DataNumber??"")
                ,new DbColumn("MeasureTime",DbType.DateTime,data.MeasureTime)
                ,new DbColumn("MeasureValue",DbType.Single,data.MeasureValue)
                ,new DbColumn("Meal",data.Meal)
                ,new DbColumn("Attribute",data.Attribute)
            };
            Database db = DatabaseFactory.CreateDatabase(_conName);
            using (TransactionScope tx = new TransactionScope())
            {
                int rds = DbHelper.ExecuteInsert(db, null, SQL.TABLE.RW_GL, columns);
                tx.Complete();
            }
            return new ReturnValue();
        }

        public ReturnValue CollectSmokeAlarmSignal(AlarmSignal data)
        {
            return CollectAlarmSignal(data, AlarmType.SmokeAlarm);
        }

        public ReturnValue CollectGasAlarmSignal(AlarmSignal data)
        {
            return CollectAlarmSignal(data, AlarmType.GasAlarm);
        }
    }
}
